using System.Net.Http.Headers;
using System.Text;
using System.Text.Json;

namespace TestApiWithIntUserId
{
    class Program
    {
        static async Task Main(string[] args)
        {
            try
            {
                Console.WriteLine("测试修复后的AddFile接口(UserId为int类型)...");
                
                // 创建HttpClient
                using var httpClient = new HttpClient();
                httpClient.BaseAddress = new Uri("http://localhost:5269/");
                httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                
                // 构造请求体 - 不包含UserId字段，因为它是可选的
                var requestBody = new
                {
                    FileName = "测试文件.txt",
                    SavedPath = "test/path/test.txt",
                    FileTypeId = 1L,
                    FtpPath = "ftp/test/path/test.txt",
                    Time = DateTime.Now,
                    OpenTime = DateTime.Now
                };

                Console.WriteLine("注意：本次测试不包含UserId字段，因为UserId是可选的");
                
                // 序列化请求体
                string jsonContent = JsonSerializer.Serialize(requestBody);
                var content = new StringContent(jsonContent, Encoding.UTF8, "application/json");
                
                Console.WriteLine($"请求体内容: {jsonContent}");
                Console.WriteLine($"发送请求到: http://localhost:5269/api/file/add");
                
                // 发送请求
                var response = await httpClient.PostAsync("api/file/add", content);
                
                // 获取响应内容
                string responseContent = await response.Content.ReadAsStringAsync();
                
                // 输出结果
                Console.WriteLine($"响应状态码: {response.StatusCode}");
                Console.WriteLine($"响应内容: {responseContent}");
                
                if (response.IsSuccessStatusCode)
                {
                    Console.WriteLine("✅ 测试成功！AddFile接口调用成功。");
                }
                else
                {
                    Console.WriteLine("❌ 测试失败！AddFile接口调用失败。");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"发生异常: {ex.Message}");
                Console.WriteLine(ex.StackTrace);
            }
            
            Console.WriteLine("按任意键退出...");
            Console.ReadKey();
        }
    }
}